﻿using MyConfig.Serialize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;

namespace MyConfig.Configuration
{
    /// <summary>
    /// 加载系统设置的配置信息
    /// </summary>
    internal class Config
    {
        /// <summary>
        /// 当前垛位配置
        /// </summary>
        public static Config Current { get; private set; }

        /// <summary>
        /// 初始化当前配置
        /// </summary>
        static Config()
        {
            Current = Load();
        }

        /// <summary>
        /// 加载系统配置信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public static Config Load()
        {
            var filename = Path.Combine(Application.StartupPath, "MyConfig.json"); // 获取系统设置文件
            string str = File.ReadAllText(filename);
            var config = JsonHelper.ToObject<Config>(str) ?? null;  // 打开文件读取文件所有内容
            if (config == null)
            {
                throw new InvalidOperationException("无法加载配置文件");
            }
            return config;
        }

        /// <summary>
        /// 单元端数据库连接字符串
        /// </summary>
        public string Database { get; set; }
        /// <summary>
        /// 工作目录
        /// </summary>
        public string WorkDir { get; set; }

    }
}
